﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Entity;

namespace EZCRS.HCMS.Web.manage.hotel
{
    public partial class seasonlist_add : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            
            if (!Page.IsPostBack)
            {
                CFun.SetListControl(CheckBoxList1, EZCRS.HCMS.Bll.baseinfo.HotelBaseInfo.Weekly(), "");

                int iID = 0;

                if (Request.QueryString["id"] != null)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                        int.TryParse(Request.Params["id"], out iID);

                    BindData(iID);
                }

                startdate.Attributes.Add("onclick", "event.cancelBubble=true;showCalendarTA('startdate',false,'startdate','startdate',null);");
                startdate.Attributes.Add("onblur", "if(!chkdate(this.value))this.value='yyyy-MM-dd';");

                enddate.Attributes.Add("onclick", "event.cancelBubble=true;showCalendarTA('enddate',false,'enddate','enddate',null);");
                enddate.Attributes.Add("onblur", "if(!chkdate(this.value))this.value='yyyy-MM-dd';");
            }
        }

        private void BindData(int ID)
        {
            BllRatePlan bllPlan = new BllRatePlan();

            HCRS_Season obj =  bllPlan.getSeasonPlan(ID);


            if (obj != null)
            {
                txtName.Text = obj.Name;
                txtDesc.Text = obj.Description;
                startdate.Value = obj.StartDate.ToString("yyyy-MM-dd");
                enddate.Value = obj.EndDate.ToString("yyyy-MM-dd");

                string [] weeks = obj.Week.Split(',');

                foreach (ListItem li in CheckBoxList1.Items)
                {
                    foreach (string day in weeks)
                        if (li.Value == day)
                            li.Selected = true;
                }

            }
            
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            HCRS_Season obj = new HCRS_Season();

            DateTime dtStart = new DateTime();
            DateTime dtEnd = new DateTime();

            DateTime.TryParse(startdate.Value, out dtStart);
            DateTime.TryParse(enddate.Value, out dtEnd);

            if (dtStart > dtEnd)
            {
                Response.Write(CFun.AlertMeg("日期选择错误！", "back", ""));
                return;
            }

            int id = CFun.RequestNumber("id");

            if (id > 0)
                obj.ID = id;

            obj.StartDate = dtStart;
            obj.EndDate = dtEnd;

            obj.Name = txtName.Text.Trim();
            obj.Description = txtDesc.Text.Trim();

            obj.PropertyID = base.BasPropertyId;

            string strWeek = "";

            foreach (ListItem li in CheckBoxList1.Items)
            {
                if (li.Selected)
                    strWeek += li.Value + ",";
            }

            obj.Week = strWeek.TrimEnd(',');

            BllRatePlan bllPlan = new BllRatePlan();

            bool b = bllPlan.SaveSeasonPlan(obj);

            if (b)
                Response.Redirect("seasonlist.aspx");
        }
    }
}
